package com.haitaoit.chengquan.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.ListView;

import java.io.File;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/4/13.
 */
public  class MyUtils {
    public static int px2dip(Context context, float pxValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5f);
    }
    public static int dip2px(Context context, float dipValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }

    public static int compareDate(Date date,Date date2){
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(date2);
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        return Math.abs(day2 - day1);
    }
    public static int compareNowDate(Date date){
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(new Date());
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        return Math.abs(day2 - day1);
    }
    public static int compareNowDate(String date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar aCalendar = Calendar.getInstance();
        try {
            aCalendar.setTime(sdf.parse(date));
            int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
            aCalendar.setTime(new Date());
            int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
            return Math.abs(day2 - day1);
        } catch (ParseException e) {
            e.printStackTrace();
            return 99;
        }
    }
    public static String dateFormat(String date,String formatString){
        SimpleDateFormat sdf1=new SimpleDateFormat("yyy-MM-dd");
        SimpleDateFormat sdf2=new SimpleDateFormat(formatString);
        try {
            String format=sdf2.format(sdf1.parse(date));
            return format;
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }
    public static Date stringToDate(String strDate)
    {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            return df.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }

    }
    public static String dateFormat(String date){
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");
        try {
            String format=sdf.format(sdf.parse(date));
            return format;
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }
    public static String getFormatDate(Date date){
        return getFormatDate(date,"yyyy-MM-dd");
    }
    public static String getFormatDate(Date date,String formatStr){
        SimpleDateFormat sdf=new SimpleDateFormat(formatStr);
        try {
            String format=sdf.format(date);
            return format;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    public static String getFormatDate(long lDate){
        return getFormatDate(lDate,"yyyy-MM-dd");
    }
    public static String getFormatDate(long lDate,String formatStr){
        SimpleDateFormat sdf=new SimpleDateFormat(formatStr);
        try {
            String format=sdf.format(new Date(lDate));
            return format;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static int getHeight(Context context){
        WindowManager wm = ((Activity)context).getWindowManager();
        return wm.getDefaultDisplay().getHeight();
    }
    public static int getWidth(Context context){
        WindowManager wm = ((Activity)context).getWindowManager();
        return wm.getDefaultDisplay().getWidth();
    }
    public static double division(double d1,
                             double d2,int len) {// 进行除法运算
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    public static double division(String d1,String d2,int len) {// 进行除法运算
        double dl1=Double.parseDouble(d1);
        double dl2=Double.parseDouble(d2);
        return division(dl1, dl2, 2);
    }
    public static double division(String d1,String d2) {// 进行除法运算
        return division(d1,d2,2);
    }
    public static void LogI(String d1,String d2) {
        Log.i("request=====log-key["+d1,"]########["+d2+"]");
    }
    public static void LogI(String d1) {
        Log.i("request","json====="+d1+"");
    }
    public static void showToast(Context context,String msg){
        if(context!=null){
            if(msg!=null&&msg.length()>0){
                ToastUtils.showToast(msg);
            }
        }
    };

    public static void setListHeight(Adapter mSearch, ListView mListView) {
        int totalHeight = 0;
        for (int i = 0, len = mSearch.getCount(); i < len; i++) {
            View listItem = mSearch.getView(i, null, mListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = mListView.getLayoutParams();
        params.height = totalHeight + (mListView.getDividerHeight() * (mSearch.getCount() - 1));
        mListView.setLayoutParams(params);
    }



    /**
     * 获取图片的旋转角度
     *
     * @param path 图片绝对路径
     * @return 图片的旋转角度
     */
    public static int getBitmapDegree(String path) {
        int degree = 0;
        try {
            // 从指定路径下读取图片，并获取其EXIF信息
            ExifInterface exifInterface = new ExifInterface(path);
            // 获取图片的旋转信息
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
            return degree;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static Bitmap toTurn(Bitmap img, int degree){
        Matrix matrix = new Matrix();
        matrix.postRotate(degree); /*翻转90度*/
        int width = img.getWidth();
        int height =img.getHeight();
        img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
        return img;
    }
    public static long getFileSize(String fileName) {
        long lgth = 0;
        File file = new File(fileName);
        if (file.exists()) {
            lgth = file.length();
        }
        file = null;
        return lgth;
    }
}
